import axios from 'axios'
const verify = {
    data() {
        return {
            agentId: '',
            corpId: 'dingc4cffb655c00d67f35c2f4657eb6378f',
            timeStamp: '',
            nonceStr: '',
            signature: '',
            appPic: '', //是否开启过渡页，0 关闭 1 开启
            verifyStatus:'', //缓存中是否已经有登录信息
        }
    },
    methods: {
        // 获取签名
        $_getGenerateSignature() {
            let _self = this;
            let postData = {
                company_id: _self.corpId,
                url: window.location.href
            };
            let url = 'user/home/v1/login/generateSignature';
            _self.$_post(url, postData).then(function(res) {
            //获取加密数据
                let resData = res.data;
                _self.agentId = resData.agentId;
                _self.timeStamp = resData.timeStamp;
                _self.nonceStr = resData.nonceStr;
                _self.signature = resData.signature;
                //jsapi鉴权并免登
                dd.config({
                    agentId: _self.agentId,
                    corpId: _self.corpId,
                    timeStamp: _self.timeStamp,
                    nonceStr: _self.nonceStr,
                    signature: _self.signature,
                    betaURL: 1,
                    jsApiList: [
                        'runtime.permission.requestAuthCode',
                    ]
                });
                dd.ready(function() {
                    _self.$_doLogin();
                });
                dd.error(function(error) {
                    console.log('error: ' + JSON.stringify(error));
                    let msg = error.errorMessage;
                    //后台记录错误日志 xiangzi 2018-7-13
                    let url = 'user/home/v1/Log';
                    let postData = {
                        company_id: _self.corpId,
                        request_url: window.location.href,
                        code: error.errorCode,
                        message: JSON.stringify(error)
                    };
                    _self.$_post(url, postData).then(function(res) {});
                });
            });
        },
        $_doLogin() {
            let _self = this;
            let authorization = window.localStorage.getItem('Authorization:' + _self.corpId);
            window.localStorage.setItem('corpId', _self.corpId);
            if (authorization) {
                window.localStorage.setItem('Authorization', authorization);
                this.$_loginRecord();
            }else{
                //获取授权信息
                dd.ready(function() {
                    dd.runtime.permission.requestAuthCode({
                        corpId: _self.corpId,
                        onSuccess: function(info) {
                            _self.$_loginRecord(info.code);
                        },
                        onFail: function(error) {
                            console.log('fail: ' + JSON.stringify(error));
                            let msg = error.errorMessage;
                            //这里添加弹窗退出应用
                            //后台记录错误日志 xiangzi 2018-7-13
                            let url = 'user/home/v1/Log';
                            let postData = {
                                company_id: _self.corpId,
                                request_url: window.location.href,
                                code: error.errorCode,
                                message: JSON.stringify(error)
                            };
                            _self.$_post(url, postData).then(function(res) {});
                        }
                    });
                });
            }
        },
        // 记录用户登录日志
        $_loginRecord() {
            let _self = this;
            window.localStorage.setItem('Authorization:' + _self.corpId, _self.corpId);
            _self.$router.push({
                name: 'success'
            });
        },
        // 免登
        $_login() {
            this.$_doLogin();
        },
    }
}
export default verify